package team1.hdhnewsapp.stockmarketactivity;

import info.androidhive.slidingmenu.R;

import java.util.ArrayList;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;

public class USAMarketFragment extends Fragment {
	private StockServiceHelper helper;
	private String[] sym2 = { "YHOO", "AAPL", "GOOG", "MSFT" };
	ListView lv;
	ArrayList<String> kq2 = new ArrayList<String>();
	ProgressBar pro;
	ArrayAdapter<String> adapter2;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_stockusa, container,
				false);
		lv = (ListView) rootView.findViewById(R.id.list1);
		pro = (ProgressBar) rootView.findViewById(R.id.probar1);
		pro.setVisibility(View.VISIBLE);
		adapter2 = null;
		for (int i = 0; i < sym2.length; i++) {
			helper = new StockServiceHelper(sym2[i]);
			new ContactWebservice().execute(helper);
		}
		kq2.clear();

		return rootView;
	}

	private class ContactWebservice extends
			AsyncTask<StockServiceHelper, String, Void> {

		@Override
		protected void onPreExecute() {
			pro.setVisibility(View.VISIBLE);
		}

		@Override
		protected Void doInBackground(StockServiceHelper... params) {
			params[0].callService();
			String kq = "Company: " + params[0].getName() + "\n\t - Price: "
					+ params[0].getLast() + "\n\t - Date: "
					+ params[0].getCurDate() + "\n\t - Change: "
					+ params[0].getChange() + "\n\t - Open: "
					+ params[0].getOpen() + "\n\t - High: "
					+ params[0].getOpen() + "\n\t - Low: " + params[0].getLow()
					+ "\n\t - PercentageChange: "
					+ params[0].getPercentageChange();
			publishProgress(kq);
			return null;
		}

		@Override
		protected void onProgressUpdate(String... values) {
			String kq3 = values[0];
			kq2.add(kq3);
			adapter2 = new ArrayAdapter<String>(getActivity(),
					android.R.layout.simple_list_item_1, kq2);
			lv.setAdapter(adapter2);
			if (kq2.size() == sym2.length) {
				pro.setVisibility(View.INVISIBLE);
			}
		}

		@Override
		protected void onPostExecute(Void result) {
		}
	}
}
